package vip.xiaonuo.xinmiao.modular.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.auth.core.annotation.SaClientCheckLogin;
import vip.xiaonuo.auth.core.pojo.SaBaseClientLoginUser;
import vip.xiaonuo.auth.core.util.StpClientLoginUserUtil;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.dev.api.DevFileApi;
import vip.xiaonuo.xinmiao.modular.userFace.entity.XmUserFace;
import vip.xiaonuo.xinmiao.modular.userFace.param.XmUserFaceAddParam;
import vip.xiaonuo.xinmiao.modular.userFace.service.XmUserFaceService;

import javax.annotation.Resource;

@Api(tags = "用户端人脸相关控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@RequestMapping("/client/c/userFace")
@Validated
public class XmClientUserFaceController {

    @Resource
    private DevFileApi devFileApi;
    @Resource
    private XmUserFaceService xmUserFaceService;

    @ApiOperationSupport
    @ApiOperation("获得用户人脸对应路径")
    @SaClientCheckLogin
    @GetMapping("/getUserFace")
    public CommonResult<String> getUserFace() {
        String userId = StpClientLoginUserUtil.getClientLoginUser().getId();
        QueryWrapper<XmUserFace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(XmUserFace::getUserId, userId);
        XmUserFace userFace = xmUserFaceService.getOne(queryWrapper);
        if (userFace == null) {
            throw new CommonException("当前用户暂时无人脸照片，请点击'更换'上传");
        }
        String ret = devFileApi.getOriFileUrl(userFace.getFaceFileId());
        return CommonResult.ok(ret);
    }

    @SneakyThrows
    @ApiOperationSupport
    @ApiOperation("上传用户人脸")
    @SaClientCheckLogin
    @PostMapping("/uploadUserFace")
    public CommonResult<String> uploadUserFace(MultipartFile file) {
        String fileId = devFileApi.storageFileWithReturnIdLocal(file);
        String userId = StpClientLoginUserUtil.getClientLoginUser().getId();
        // 先删除旧的关联
        QueryWrapper<XmUserFace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(XmUserFace::getUserId, userId);
        xmUserFaceService.remove(queryWrapper);

        // 再添加新的
        XmUserFaceAddParam addParam = new XmUserFaceAddParam();
        addParam.setFaceFileId(fileId);
        addParam.setUserId(userId);
        xmUserFaceService.add(addParam);
        return CommonResult.ok();
    }


}
